package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.DiseaseFileDao;
import cn.topevery.garden.entity.po.DiseaseFile;
import cn.topevery.garden.entity.query.DiseaseFileQuery;
import cn.topevery.garden.entity.vo.DiseaseFileVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.basis.runtime.UserRuntime;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.entity.SqlUpdateMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 病虫害附件
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class DiseaseFileDaoImpl extends BaseDaoImpl<DiseaseFile> implements DiseaseFileDao {
    @Override
    public <P extends DbCsp> List<DiseaseFile> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<DiseaseFileVO> listPage(DiseaseFileQuery query) {
        String table = "t_disease_file tdf";

        String columns = "tdf.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tdf.c_id desc");
        sqlQueryMate
                .where(true, "tdf.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, DiseaseFileVO.class);
    }

    @Override
    public DiseaseFile getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public List<DiseaseFile> getListByMainId(Integer mainId) {
        String table = "t_disease_file tdf";
        String columns = "tdf.*";
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate.where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(mainId != null, "c_main_id", ScriptConditionEnum.Eq, mainId);
        return selectList(sqlQueryMate, DiseaseFile.class);
    }

    @Override
    public void insert(DiseaseFile diseaseFile) {
        insert(UserRuntime.getSysUser().userId, diseaseFile);
    }

    @Override
    public void insertList(List<DiseaseFile> list) {
        insert(UserRuntime.getSysUser().userId, list);
    }

    @Override
    public void deleteByMainId(Integer mainId, Integer userId) {
        SqlUpdateMate sqlUpdateMate = new SqlUpdateMate("t_disease_file");
        sqlUpdateMate.setColumnValue(true, "c_db_status", 1)
                .setColumnValue(userId != null, "c_db_update_id", userId)
                .setColumnValue(true, "c_db_update_date", LocalDateTime.now());
        sqlUpdateMate.where(mainId != null, "c_main_id", ScriptConditionEnum.Eq, mainId);
        run(sqlUpdateMate.getSql(), CommandType.Text, dbCommand -> {
            sqlUpdateMate.getParameters().forEach(dbCommand::addParameter);
            dbCommand.executeNonQuery();
        });
    }
}
